package logic

import (
	"context"
	"fmt"
	"vipshop/pkg"
	"vipshop/vipshoprpc/models/mysql"

	"vipshop/vipshoprpc/internal/svc"
	"vipshop/vipshoprpc/vipshoprpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type RegisterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterLogic {
	return &RegisterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RegisterLogic) Register(in *vipshoprpc.RegisterRequest) (*vipshoprpc.RegisterResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.User
	mysql.DB.Debug().Where("phone=?", in.Phone).Find(&user)
	if user.ID != 0 {
		return nil, fmt.Errorf("账号已存在,请前去注册")
	}
	uses := mysql.User{

		UserName: in.Username,
		Phone:    in.Phone,
		Password: pkg.Md5(in.Password),
	}
	mysql.DB.Debug().Create(&uses)
	return &vipshoprpc.RegisterResponse{
		Id: int64(uses.ID),
	}, nil
}
